package org.dromara.comm.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dromara.comm.domain.AsHome;
import org.dromara.comm.domain.AsHomeClassName;
import org.dromara.comm.mapper.AsHomeClassNameMapper;
import org.dromara.comm.mapper.AsHomeMapper;
import org.dromara.comm.service.AsHomeService;
import org.dromara.comm_lhz.util.QiniuUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 到家服务
 * @author: 郭伟明
 * @date: 2024-08-09 17:27
 **/
@Service
public class AsHomeServiceImpl extends ServiceImpl<AsHomeMapper, AsHome> implements AsHomeService {

    @Autowired
    private AsHomeMapper asHomeMapper;

    @Autowired
    private AsHomeClassNameMapper asHomeClassNameMapper;

    @Autowired
    private QiniuUtil qiniuUtil;

    @Override
    public List<AsHomeClassName> queryHome() {
        return asHomeClassNameMapper.selectList(null);
    }

    @Override
    public List<Map<String, Object>> queryHomeByClassName(AsHomeClassName asHomeClassName) {
        List<Map<String, Object>> resultList = new ArrayList<>();

        // 构建查询条件，查询所有 AsHomeClassName 记录
        LambdaQueryWrapper<AsHomeClassName> classWrapper = new LambdaQueryWrapper<>();
        if (asHomeClassName.getClassName() != null) {
            classWrapper.eq(AsHomeClassName::getClassName, asHomeClassName.getClassName());
        }
        List<AsHomeClassName> classDetailsList = asHomeClassNameMapper.selectList(classWrapper);

        // 遍历所有查询到的 AsHomeClassName 记录
        for (AsHomeClassName classDetails : classDetailsList) {
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("id", classDetails.getId());
            resultMap.put("className", classDetails.getClassName());

            // 查询与当前 classDetails 关联的 AsHome 项目
            LambdaQueryWrapper<AsHome> homeWrapper = new LambdaQueryWrapper<>();
            homeWrapper.eq(AsHome::getClassId, classDetails.getId());
            List<AsHome> asHomeList = asHomeMapper.selectList(homeWrapper);

            resultMap.put("asHome", asHomeList);
            resultList.add(resultMap);
        }

        return resultList;
    }

    @Override
    public void insertHome(AsHome asHome) {
        //传classId=1
        this.save(asHome);
    }

    @Override
    public void updateHome(AsHome asHome) {
        //传classId=1
        this.updateById(asHome);
    }
}
